import { Notification } from 'element-ui'

export default function ({ $axios, redirect }) {
  $axios.onRequest(config => {
    console.log('请求接口', config.url);
  });
  $axios.onResponse((response) => {
    switch (response.data.status) {
      case 200:
        console.log('响应信息', response);
        break;
      case 300:
        console.log('响应信息', response);
        break;
      case 500:
        Notification.error({
          title: response.data.message,
          message: response.data.error,
          duration: 0
        });
        break;
      default:
        Notification.error({
          title: '提交失败',
          message: '网络中断，请稍后再重试！',
          duration: 0
        });
        break;
    }
  });
  $axios.onError((error) => {
    const code = parseInt(error.response && error.response.status);
    if (code === 400) {
      redirect('/400');
    }
  });
  $axios.onRequestError((error) => {
    console.log('请求错误', error);
  });
  $axios.onResponseError((error) => {
    console.log('响应错误', error);
    Notification.error({
      title: '获取数据失败！',
      message: '服务器出现问题，请联系管理员！',
      duration: 0
    });
  });
}
